# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
'''
外键冲突未解决
'''
'''
姚明 281720252@qq.com
'''


class CardAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryCard(self, userId):  # 根据userId查询出所有的银行卡返回给上级函数
        cur = self.conn.cursor()
        sql = "SELECT * FROM zzh_card WHERE user_id = %s"
        queryResultOfDatabaseCard = cur.execute(sql, userId)
        cardList = cur.fetchall()
        cardListJson = Table2Json.card_list_to_json(cardList)
        if queryResultOfDatabaseCard:
            cur.close()
            self.conn.close()
            return queryResultOfDatabaseCard, cardListJson

        else:
            cur.close()
            self.conn.close()
            return 0

    def addCard(self, addCard):  # process表添加记录
        userId = addCard['userId']  # processChildName  子流程的名称
        cardName = addCard['cardName']  # processChildCode 子流程步数
        cardTel = addCard['cardTel']  # processChildStatus  子流程状态
        cardType = addCard['cardType']
        cardCode = addCard['cardCode']
        userType = addCard['userType']
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_card (user_id,card_name,card_phone,card_type,card_code,user_type) values (%s,%s,%s,%s,%s,%s)"
        addResultOfDatabaseCard = cur.execute(sql, [userId, cardName, cardTel, cardType, cardCode, userType])
        self.conn.commit()
        if addResultOfDatabaseCard:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateCard(self, updateCard, cardId):  # process表添加记录
        userId = updateCard['userId']  # processChildName  子流程的名称
        cardName = updateCard['cardName']  # processChildCode 子流程步数
        cardTel = updateCard['cardTel']  # processChildStatus  子流程状态
        cardType = updateCard['cardType']
        cardCode = updateCard['cardCode']
        userType = updateCard['userType']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_card SET user_id=%s,card_name=%s,card_phone=%s,card_type=%s,card_code=%s,user_type=%s WHERE card_id=%s"
        updateResultOfDatabaseCard = cur.execute(sql, [userId, cardName, cardTel, cardType, cardCode, userType, cardId])
        self.conn.commit()
        if updateResultOfDatabaseCard:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteCardById(self, cardId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_card WHERE card_id=%s"
        deleteCardResultOfDatabase = cur.execute(sql, cardId)
        self.conn.commit()
        if deleteCardResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0
